package com.abc.recycling.controller;

import com.abc.recycling.model.Order;
import com.abc.recycling.model.RecyclingStation;
import com.abc.recycling.service.OrderService;
import com.abc.recycling.service.RecyclingStationService;
import com.abc.recycling.service.UserService;
import com.abc.recycling.mapper.RecyclingStationMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Controller
@RequestMapping("/resident")
@RequiredArgsConstructor
public class ResidentController {

    private final OrderService orderService;
    private final UserService userService;
    private final RecyclingStationService stationService;
    private final RecyclingStationMapper stationMapper;

    @GetMapping({"", "/"})
    public String index(Model model) {
        try {
            log.info("开始加载居民首页数据");
            
            // 获取最近订单
            log.debug("正在获取最近订单");
            List<Order> recentOrders = orderService.getRecentOrders(5);
            log.info("获取到{}个最近订单", recentOrders.size());
            model.addAttribute("recentOrders", recentOrders);
            
            // 获取所有营业中的回收站
            log.debug("正在获取营业中的回收站");
            List<RecyclingStation> stations = stationMapper.findAllActive();
            log.info("找到{}个营业中的回收站", stations.size());
            
            if (stations.isEmpty()) {
                log.warn("没有找到任何营业中的回收站");
            } else {
                stations.forEach(station -> 
                    log.info("回收站信息: [id={}, 名称={}, 地址={}, 状态={}, 经度={}, 纬度={}]", 
                        station.getId(),
                        station.getName(), 
                        station.getAddress(), 
                        station.getStatus(),
                        station.getLongitude(),
                        station.getLatitude()));
            }
                
            model.addAttribute("nearbyStations", stations);
            log.info("居民首页数据加载完成");
                
            return "resident/index";
        } catch (Exception e) {
            log.error("加载首页数据失败: {}", e.getMessage(), e);
            return "error/500";
        }
    }

    @GetMapping("/orders")
    public String orders(Model model, Authentication authentication) {
        Long userId = userService.getUserByUsername(authentication.getName()).getId();
        List<Order> orders = orderService.getOrdersByUserId(userId);
        model.addAttribute("orders", orders);
        return "resident/orders";
    }

    @GetMapping("/station")
    public String stationList(Model model) {
        // 获取所有回收站
        List<RecyclingStation> allStations = stationService.findStations(null);
        model.addAttribute("stations", allStations);
        return "resident/station/list";
    }
}